#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年7月3日

@author: chenyongbing
'''
import sys,os
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

import logging


from logging.handlers import RotatingFileHandler

def change_level(level = 'info'):
    if type(level).__name__ == 'str':
        if level.lower() == 'debug':
            level_num =   logging.DEBUG
        elif level.lower() == 'info':
            level_num = logging.INFO
        elif level.lower() == 'warn' or level.lower() == 'warning':
            level_num = logging.WARN
        elif level.lower() == 'error':
            level_num = logging.ERROR
        elif level.lower() == 'critical':
            level_num = logging.CRITICAL
        elif level.lower() == 'fatal':
            level_num = logging.FATAL
        else:
            level_num = logging.DEBUG
    else:
        level_num = level
    print level , level_num
    return level_num

# def logging_stdout(level='info',
#                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                    logger_name=''
#                    ):
#     
#     formatter = logging.Formatter(format)
#     
# #     logging.basicConfig(level=level,format=format)
# #      
#     console = logging.StreamHandler()
#     console.setLevel(change_level(level))
#     console.setFormatter(formatter)
#     logging.getLogger('').addHandler(console)
#     logger = logging.getLogger(logger_name)
#     logger.setLevel(change_level(level))
#     return logger
# 
# 
# def logging_logfile(level='info',
#                    logfile='stdout.log',
#                    maxBytes=10*1024*1024,
#                    backupCount=0,
#                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                    logger_name=''):
#     Rthandler = RotatingFileHandler(logfile, maxBytes=maxBytes,backupCount=backupCount)
#     Rthandler.setLevel(change_level(level))
#     formatter = logging.Formatter(format)
#     Rthandler.setFormatter(formatter)
#     logging.getLogger('').addHandler(Rthandler)
#     logger = logging.getLogger(logger_name)
#     logger.setLevel(change_level(level))
#     return logger
# 
# def logging_stdout_and_logfile(level='info',
#                                logfile='stdout.log',
#                                maxBytes=10*1024*1024,
#                                backupCount=0,
#                                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
#                                logger_name=''):
#     logging_logfile(level=level,logfile=logfile,maxBytes=maxBytes,backupCount=backupCount,format=format,logger_name=logger_name)
#     logging_stdout(level=level,format=format,logger_name=logger_name)
#     logger = logging.getLogger(logger_name)
#     logger.setLevel(change_level(level))
#     
#     return logger


def logging_stdout(level='info',
                   format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                   logger_name=''
                   ):
    
    level = change_level(level)
    
    logging.basicConfig(level=level)
    formatter = logging.Formatter(format)
    console = logging.StreamHandler()
    console.setFormatter(formatter)
    logging.getLogger(logger_name).addHandler(console)
     
    return logging
      

 
 
def logging_logfile(level='info',
                   logfile='stdout.log',
                   maxBytes=10*1024*1024,
                   backupCount=0,
                   format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                   logger_name=''):
    logging.basicConfig(level= logging.INFO)
    
    Rthandler = RotatingFileHandler(logfile, maxBytes=maxBytes,backupCount=backupCount)
    formatter = logging.Formatter(format)
    Rthandler.setFormatter(formatter)
    logging.getLogger(logger_name).addHandler(Rthandler)
    return logging
 
def logging_stdout_and_logfile(level='info',
                               logfile='stdout.log',
                               maxBytes=10*1024*1024,
                               backupCount=0,
                               format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                               logger_name=''):
    level = change_level(level)
    
    logging.basicConfig(level=level)
    
    Rthandler = RotatingFileHandler(logfile, maxBytes=maxBytes,backupCount=backupCount)
    formatter = logging.Formatter(format)
    Rthandler.setFormatter(formatter)
    logging.getLogger(logger_name).addHandler(Rthandler)
    
    
    formatter = logging.Formatter(format)
    console = logging.StreamHandler()
    console.setFormatter(formatter)
    logging.getLogger(logger_name).addHandler(console)
    
    return logging


